﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETDBModel;

/// <summary>
/// Summary description for BL
/// </summary>
public class BL
{
    DAL dl = new DAL();

	public BL()
	{
	}

    /// <summary>
    /// Get Question by Page
    /// </summary>
    /// <param name="QaA">Session["allAnswAndQuest"]</param>
    /// <param name="page">Session["page"]</param>
    /// <returns>Question</returns>
    public aspnet_TestQuestions GetCurrentQuestionFromSession(object QaA, object page)
    {
        return (QaA as Dictionary<aspnet_TestQuestions, List<aspnet_Answers>>).Keys.ElementAt(Convert.ToInt32(page) - 1);
    }


    /// <summary>
    /// Get Answers by Page
    /// </summary>
    /// <param name="QaA">Session["allAnswAndQuest"]</param>
    /// <param name="page">Session["page"]</param>
    /// <returns>array of Answers</returns>
    public List<aspnet_Answers> GetCurrentAnswersFromSession(object QaA, object page)
    {
        return (QaA as Dictionary<aspnet_TestQuestions, List<aspnet_Answers>>).Values.ElementAt(Convert.ToInt32(page) - 1);
    }

    /// <summary>
    /// Create Dictionary where key is Question and value is array of Anwer
    /// </summary>
    /// <returns>Dictionary</returns>
    public Dictionary<aspnet_TestQuestions, List<aspnet_Answers>> CreateDictAllAnswAndQuest(string username, string idtest)
    {
        Dictionary<aspnet_TestQuestions, List<aspnet_Answers>> dictAnswerAndQuest = new Dictionary<aspnet_TestQuestions, List<aspnet_Answers>>();

        List<aspnet_TestQuestions> listQuestions = dl.SelectQuestionsByTest(idtest);
        foreach (aspnet_TestQuestions quest in listQuestions)
        {
            List<aspnet_Answers> tempAnswers = dl.SelectAnswersByQuestion(quest.QuestionId.ToString());
            dictAnswerAndQuest.Add(quest, tempAnswers);
        }
        return dictAnswerAndQuest;
    }

    /// <summary>
    /// Create Dictionary where key is Category and value is array of Tests
    /// </summary>
    /// <returns>Dictionary</returns>
    public  Dictionary<aspnet_TestCategory, List<aspnet_Tests>> CreateDictAllCatAndTests()
    {
        Dictionary<aspnet_TestCategory, List<aspnet_Tests>> dictCatAndTests=new Dictionary<aspnet_TestCategory,List<aspnet_Tests>>();
        List<aspnet_TestCategory> tempAllCat = dl.SelectAllCategories();
        foreach (aspnet_TestCategory cat in tempAllCat)
        {
            dictCatAndTests.Add(cat, dl.SelectTestsByCategoryId(cat.CategoryId.ToString()));
        }
        return dictCatAndTests;
    }


}